package com.cloudbroker.openapi.manage.controller;

import static com.cloudbroker.bcs.platform.dav.common.util.RequestUtil.getSingleParameterMapForBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.common.util.ValueUtil;
import com.cloudbroker.bcs.platform.dav.bizapi.service.base.vo.HSBCPSVO;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.fund.result.FuncFundDetailExportResult;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.fund.result.FundIncomeResult;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.fund.result.FundListData;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.fund.result.FundListResult;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.fund.result.FundRewardAmountResult;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.fund.vo.FundDetailExportForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.fund.vo.FundExportForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.fund.vo.FundListQueryForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.fund.vo.FundUf30ExportForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.fund.vo.QryRewardForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.gold.result.GoldJourData;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.gold.result.UserGoldJourResult;
import com.cloudbroker.bcs.platform.dav.bizapi.t2service.business.FundT2BizService;
import com.cloudbroker.bcs.platform.dav.common.ext.annotation.SaveLog;
import com.cloudbroker.bcs.platform.dav.common.util.ExcelOut;

@Controller
public class FundController {
    
    @Resource
    private FundT2BizService fundT2;
    
    /**
     * 查询收入
     */
    @ResponseBody
    @RequestMapping("/func_fund_income")
    @SaveLog(remark = "查询收入")
    public FundIncomeResult funcFundIncome(HttpServletRequest request, String jsonpcallback) {
        HSBCPSVO form = getSingleParameterMapForBody(request, HSBCPSVO.class);
        return VOUtil.setJsonpResult(fundT2.funcFundIncome(form), jsonpcallback);
    }
    
    /**
     * 查询收入列表
     */
    @ResponseBody
    @RequestMapping("/func_fund_list")
    @SaveLog(remark = "查询收入列表")
    public FundListResult funcFundList(HttpServletRequest request, String jsonpcallback) {
        FundListQueryForm form = getSingleParameterMapForBody(request, FundListQueryForm.class);
        return VOUtil.setJsonpResult(fundT2.funcFundList(form), jsonpcallback);
    }
    
    /**
     * 查询打赏金额
     */
    @ResponseBody
    @RequestMapping("/func_qry_reward")
    @SaveLog(remark = "查询打赏金额")
    public FundRewardAmountResult funcQryReward(HttpServletRequest request, String jsonpcallback) {
        QryRewardForm form = getSingleParameterMapForBody(request, QryRewardForm.class);
        return VOUtil.setJsonpResult(fundT2.funcQryReward(form), jsonpcallback);
    }
    
    /**
     * 收入导出
     */
    @ResponseBody
    @RequestMapping("/func_fund_export")
    @SaveLog(remark = "收入导出")
    public Map<String, Object> funcFundExport(HttpServletRequest request, HttpServletResponse response,
            String jsonpcallback) {
        FundExportForm form = getSingleParameterMapForBody(request, FundExportForm.class);

        FundListResult fundListResult = this.fundT2.funcFundExport(form);
        if (fundListResult.getError_no().equals("0")) {
            List<FundListData> retList = fundListResult.getIncome_list();
            String fileName = fundListResult.getFile_name();
            String sheetName = "收入流水";
            String[] titles = { "流水时间", "手机号", "昵称", "打赏", "打赏数额" };
            Vector<Object[]> vector = new Vector<Object[]>();
            if (retList == null) {
                BigDecimal zero = new BigDecimal(0);
                try {
                    ExcelOut.exportToExcel(response, fileName, sheetName, titles, vector, zero, zero, zero, zero,
                            zero, zero);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return VOUtil.genSuccessResult();
            }
            
            BigDecimal record_count = new BigDecimal(fundListResult.getRecord_count());
            BigDecimal reward_count = new BigDecimal(fundListResult.getReward_count());
            BigDecimal viewpoint_read = fundListResult.getViewpoint_read() != null ? fundListResult
                    .getViewpoint_read() : new BigDecimal(0);
            BigDecimal question = fundListResult.getQuestion() != null ? fundListResult
                    .getQuestion() : new BigDecimal(0);
            BigDecimal comment = fundListResult.getComment() != null ? fundListResult.getComment()
                    : new BigDecimal(0);
            BigDecimal survey = fundListResult.getSurvey_reward() != null? fundListResult.getSurvey_reward()
                    : new BigDecimal(0);
            
            for (FundListData fundListData : retList) {
                Object[] excelData = { String.valueOf(fundListData.getCreate_time()),fundListData.getMobile_tel(),
                        fundListData.getCustomer_name(), String.valueOf(fundListData.getReward_type()),
                        String.valueOf(fundListData.getReward_amount()) };
                vector.add(excelData);
            }
            try {
                ExcelOut.exportToExcel(response, fileName, sheetName, titles, vector, record_count, reward_count,
                        viewpoint_read, question, comment, survey);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return VOUtil.genSuccessResult();
        } else {
            return VOUtil.genErrorResult(fundListResult.getError_no(), fundListResult.getError_info());
        }
    }
    
    @ResponseBody
    @RequestMapping("/func_fund_detail_export")
    @SaveLog(remark = "大V资金明细导出")
    public Map<String, Object> funcFundDetailExport(HttpServletRequest request, HttpServletResponse response,
            String jsonpcallback) {
        FundDetailExportForm form = getSingleParameterMapForBody(request, FundDetailExportForm.class);
        FuncFundDetailExportResult detailResult = fundT2.funcFundDetailExport(form);
        if ("0".equals(detailResult.getError_no())) {
            List<FundListData> data_list = detailResult.getData_list();
            BigDecimal reward_count = new BigDecimal(ValueUtil.getString(detailResult.getReward_count(), "0"));
            String[] titles = { "流水时间", "昵称", "打赏来源", "打赏数额" };
            Vector<Object[]> vector = new Vector<Object[]>();
            if(data_list == null){
                BigDecimal zero = new BigDecimal(0);
                try {
                    ExcelOut.exportToExcel(response, "大V资金明细", "大V资金明细导出", titles, vector, zero, zero, zero, zero,
                            zero, zero);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return VOUtil.genSuccessResult();
            }
            List<Map<String, Object>> retList = new ArrayList<Map<String, Object>>();
            for (FundListData list : data_list) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("create_time", list.getCreate_time());
                map.put("customer_name", list.getCustomer_name());
                map.put("reward_amount", list.getReward_amount());
                map.put("reward_type", list.getReward_type());
                map.put("mobile_tel", list.getMobile_tel());
                retList.add(map);
            }
            BigDecimal record_count = new BigDecimal(detailResult.getRecord_count());
            BigDecimal viewpoint_read = detailResult.getViewpoint_read() != null ? detailResult
                    .getViewpoint_read() : new BigDecimal(0);
            BigDecimal viewpoint_reward = detailResult.getViewpoint_reward() != null ? detailResult
                    .getViewpoint_reward() : new BigDecimal(0);
            BigDecimal comment = detailResult.getComment() != null ? detailResult.getComment()
                    : new BigDecimal(0);
            
            for (FundListData fundListData : data_list) {
                Object[] excelData = { String.valueOf(fundListData.getCreate_time()),
                        fundListData.getCustomer_name(), String.valueOf(fundListData.getReward_type()),
                        String.valueOf(fundListData.getReward_amount()) };
                vector.add(excelData);
            }
            try {
                BigDecimal zero = new BigDecimal(0);
                ExcelOut.exportToExcel(response, "大V资金明细", "大V资金明细导出", titles, vector, record_count, reward_count,
                        viewpoint_read, viewpoint_reward, comment, zero);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return VOUtil.genSuccessResult();
        }else{
            return VOUtil.genErrorResult(detailResult.getError_no(), detailResult.getError_info());
        }
    }
    
    @ResponseBody
    @RequestMapping("/func_fund_uf30_consume_export")
    @SaveLog(remark = "金币系统消费对账表")
    public Map<String, Object> funcFundUf30ConsumeExport(HttpServletRequest request, HttpServletResponse response,
            String jsonpcallback) {
        FundUf30ExportForm form = getSingleParameterMapForBody(request, FundUf30ExportForm.class);
        UserGoldJourResult exportResult = fundT2.funcFundUf30ConsumeExport(form);
        if ("0".equals(exportResult.getError_no())) {
            List<GoldJourData> dataList = exportResult.getData_list();
            ExcelOut.exportToUF30ConsumeExcel(response, "金币系统消费对账表", dataList);
            return VOUtil.genSuccessResult();
        }else{
            return VOUtil.genErrorResult(exportResult.getError_no(), exportResult.getError_info());
        }
    }
    
    @ResponseBody
    @RequestMapping("/func_fund_uf30_recharge_export")
    @SaveLog(remark = "金币系统充值对账表")
    public Map<String, Object> funcFundUf30RechargeExport(HttpServletRequest request, HttpServletResponse response,
            String jsonpcallback) {
        FundUf30ExportForm form = getSingleParameterMapForBody(request, FundUf30ExportForm.class);
        UserGoldJourResult exportResult = fundT2.funcFundUf30RechargeExport(form);
        if ("0".equals(exportResult.getError_no())) {
            List<GoldJourData> dataList = exportResult.getData_list();
            ExcelOut.exportToUF30RechargeExcel(response, "金币系统充值对账表", dataList);
            return VOUtil.genSuccessResult();
        }else{
            return VOUtil.genErrorResult(exportResult.getError_no(), exportResult.getError_info());
        }
    }
}
